import { defineStore } from 'pinia'
import {ref} from 'vue'
export const useCounterStore = defineStore('cart', () => {
  let carts=ref([])
    function addCart(cartdata,goodsid){
        let username=localStorage.getItem('username')
        let usercart=carts.value.filter(item=>item.username==username)
        let pdCart=usercart.find(item=>item.goods.goods_id==goodsid)
        if(pdCart){
            pdCart.number++
        }
        else{
            carts.value.push({
                username:username,
                id:Math.floor(Math.random()*100000),
                number:1,
                goods:cartdata,
                checked:false
            })
        }
    }
    function delCart(id){
        carts.value=carts.value.filter(item=>item.id!=id)
    }

  return { carts,addCart,delCart }
},
 {
    persist: true,
  },
)